home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Tcl / SystemCode / grep.tcl < prev    next >
Text File  |  1995-06-13  |  3KB  |  101 lines

  1. #================================================================================
  2. # 'greplist' and 'grepfset' are used for batch searching from the "find" dialog.
  3. #  Hence, you really shouldn't mess with them unless you know what you are doing.
  4. #================================================================================
  5. proc greplist {args} {
  6.     global tileLeft tileTop tileWidth tileHeight errorHeight
  7.  
  8.     set word [lindex $args 0]
  9.     set args [lrange $args 1 end]
  10.     
  11.     set num [expr [llength $args] - 2]
  12.     set exp [lindex $args $num]
  13.     set arglist [lindex $args [expr $num + 1]]
  14.     
  15.     set opened 0
  16.     set owin 0
  17.     set cid [scancontext create]
  18.  
  19.     set cmd [lrange $args 0 [expr $num - 1]]
  20.     eval scanmatch $cmd {$cid $exp {
  21.  
  22.             if {!$word || [regexp -nocase "(^|\[^a-zA-Z0-9\])${exp}(\[^a-zA-Z0-9\]|\$)" $matchInfo(line)]} {
  23.                 if (!$owin) {
  24.                     set owin 1
  25.                     new -n {* Batch Find *} -g $tileLeft $tileTop $tileWidth $errorHeight
  26.     
  27.                     global winModes
  28.                     set name [lindex [winNames] 0]
  29.                     changeMode [set winModes($name) Brws]
  30.     
  31.                     insertText "(<cr> to go to match)\r-----\r"
  32.                     set w [lindex [winNames -f] 0]
  33.                     set opened 1
  34.                 }
  35.                 set l [expr 20 - [string length [file tail $f]]]
  36.                 insertText -w $w "\"[file tail $f]\"[format "%$l\s" ""]; Line $matchInfo(linenum): $matchInfo(line)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t░$f\r"}
  37.             }
  38.     }
  39.  
  40.     foreach f $arglist {
  41.         message [file tail $f]
  42.         if {![catch {set fid [open $f]}]} {
  43.             scanfile $cid $fid
  44.             close $fid
  45.         }
  46.     }
  47.     scancontext delete $cid
  48.  
  49.     if {$opened} {
  50.         select [nextLineStart [nextLineStart 0]] [nextLineStart [nextLineStart [nextLineStart 0]]]
  51.         setWinInfo dirty 0
  52.         setWinInfo read-only 1
  53.     }
  54.     message ""
  55. }
  56.  
  57. proc grepfset {args} {
  58.     global fileSets
  59.  
  60.     set num [expr [llength $args] - 2]
  61.     set exp [lindex $args $num]
  62.     set fset [lindex $args [expr $num + 1]]
  63.     eval greplist [lrange $args 0 [expr $num-1]] {$exp $fileSets($fset)}
  64. }
  65.  
  66. proc grep {exp args} {
  67.     set files {}
  68.     foreach arg $args {
  69.         if {![catch {glob -t TEXT $arg} lst]} {
  70.             append files " " $lst
  71.         }
  72. #         if {![catch {glob -t ttro $arg} lst]} {
  73. #             append files " " $lst
  74. #         }
  75.     }
  76.     if {$files==""} {return $lst}
  77.     set cid [scancontext create]
  78.     scanmatch $cid $exp {
  79.         if {!$blah} {
  80.             set blah 1
  81.             set lines "(<cr> to go to match)\r"
  82.         }
  83.         set l [expr 20 - [string length [file tail $f]]]
  84.         append lines "\"[file tail $f]\"[format "%$l\s" ""]; Line $matchInfo(linenum): $matchInfo(line)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t░$f\r"
  85.     }
  86.  
  87.     set blah 0
  88.     set lines ""
  89.  
  90.     foreach f $files {
  91.         if {![catch {set fid [open $f]}]} {
  92.             message [file tail $f]
  93.             scanfile $cid $fid
  94.             close $fid
  95.         }
  96.     }
  97.     scancontext delete $cid
  98.     return [string trimright $lines "\r"]
  99. }
  100.  
  101.